package com.broadengate.dao.impl;

import java.util.ArrayList;
import java.util.List;

import com.broadengate.dao.CardDao;
import com.broadengate.model.*;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/******
 * 提供卡与数据库的交互方法
 * 方法主要有findCardByCardNO()从卡号得到卡；addCard()添加新卡；login()用卡登陆
 */
public class CardDaoImpl extends HibernateDaoSupport implements CardDao{
	@SuppressWarnings("unchecked")
	
	public List login(String cardNO,String password) {
		System.out.println("CardDaoImpl.login进入");
		List<Card> list = new ArrayList<Card>();
		list= getHibernateTemplate().find("from Card card where card.cardNo='"+cardNO+"' and card.password='"+password+"'");	
		
		return null!=list && 0!=list.size() ? list : null;
	}
	
	@SuppressWarnings("unchecked")
	public Card findCardByCardNO(String cardNO) {
		System.out.println("CardDaoImpl.findCardByCardNO进入");
		List<Card> list = new ArrayList<Card>();
		list= getHibernateTemplate().find("from Card card where card.cardNo='"+cardNO+"'");		
		return null!=list && 0!=list.size() ? (Card)list.get(0) : null;
	}
	
	public void addCard(Card card){
		
		getHibernateTemplate().save(card);
	}
	
	@SuppressWarnings("unchecked")
	public Customer findCustomerByCardNO(String cardNO){
		
		Card card=new Card();
		card=findCardByCardNO(cardNO);
		return card.getCustomer();

	}
	
	public Atminfo findAtminfo(){
		List<Atminfo> list = new ArrayList<Atminfo>();
		list= getHibernateTemplate().find("from Atminfo");		
		return null!=list && 0!=list.size() ? (Atminfo)list.get(0) : null;
	}
	
	public void updateCard(String cardNO){
		Card card=new Card();
		if(findCardByCardNO(cardNO)!=null){
		   card=findCardByCardNO(cardNO);
		   card.setCardNo(cardNO+"XXX");
		   getHibernateTemplate().update(card);
		}	
	}
}
